//
//  PersonEditCtrl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/6.
//  Copyright © 2016年 game. All rights reserved.
//

#import "PersonEditCtrl.h"
#import "PersonEditTableViewCell.h"
#import "PersonEditModel.h"
#import "ImageToController.h"
#import "PlusSignController.h"
#import "ClickCommentCtl.h"
#import "IslanInforChangeCtl.h"
#import "RootTableview.h"
@interface PersonEditCtrl ()<UITableViewDataSource,UITableViewDelegate,PersonEditTableViewCellDeleget,UpLodeImageDeleget,ClickCommentCtlDeleget,PlusSignControllerDeleget,IslanInforChangeCtlDeleget>
@property (nonatomic,strong) RootTableview *tableview;
@property (nonatomic,strong) NSMutableArray *dataAry;

@property (nonatomic,strong) MBProgressHUD *MyHUD;
@end

@implementation PersonEditCtrl

-(MBProgressHUD *)MyHUD{
    
    if (!_MyHUD) {
        _MyHUD =  [[MBProgressHUD alloc] initWithView:self.view];
        _MyHUD.dimBackground = YES;
        // 隐藏时候从父控件中移除
        //        _MyHUD.removeFromSuperViewOnHide = YES;
        
        [self.view addSubview:_MyHUD];
        _MyHUD.mode = MBProgressHUDModeDeterminate;
    }
    
    return _MyHUD;
}

-(NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
    StatusBlack;
}

- (IBAction)backClick:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = [UIScreen mainScreen].bounds;
    
    self.tableview = [[RootTableview alloc]initWithFrame:CGRectMake(0, 64, screen_width, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    self.tableview.isShowImage = YES;
    self.tableview.backgroundColor = tabGropBackColor;
    [self.tableview registerNib:[UINib nibWithNibName:@"PersonEditTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.view addSubview:self.tableview];
    self.tableview.separatorStyle = 0;
    
    self.dataAry = [NSMutableArray arrayWithContentsOfFile:[Manager editeFilePath]];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    PersonEditTableViewCell *cell = (PersonEditTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.height;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataAry.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PersonEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = [[PersonEditModel alloc]initWithDictionary:self.dataAry[indexPath.row] error:nil];
    cell.deleget = self;
    
    return cell;
}


/**
 *  删除
 *
 *  @param cell cell本身  用于获取当前cell在tableview中的indexpath
 */
-(void)deleteBtnClickWhithCell:(UITableViewCell *)cell{

//    PersonEditTableViewCell *editCell = (PersonEditTableViewCell *)cell;
    
    NSIndexPath *indexPath = [self.tableview indexPathForCell:cell];
   
        [self.dataAry removeObjectAtIndex:indexPath.row];//移除数据源的数据
    
        [self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    
    [self.dataAry writeToFile:[Manager editeFilePath] atomically:YES];
    
}

/**
 *  修改
 */
-(void)repairBtnClickWhithCell:(UITableViewCell *)cell{

    PersonEditTableViewCell *editCell = (PersonEditTableViewCell *)cell;
    
     NSIndexPath *indexPath = [self.tableview indexPathForCell:cell];
    
    if ([editCell.type isEqualToString:@"DcUpImage"]) {//钓场传图
        ImageToController *UpImage = [ImageToController new];
        UpImage.deleget = self;
        UpImage.dId = editCell.Id;
        UpImage.editDic = self.dataAry[indexPath.row];
        
        [self.navigationController pushViewController:UpImage animated:YES];
    }
    
    if ([editCell.type isEqualToString:@"PlusUp"]) {//发帖
        PlusSignController *plus = [PlusSignController new];
        plus.fid = editCell.Id;
         plus.deleget = self;
        [self presentViewController:plus animated:YES completion:^{
            plus.editDic = self.dataAry[indexPath.row];

        }];
    }
    
    if ([editCell.type isEqualToString:@"DcComment"]) {//钓场点评
        ClickCommentCtl *comment = [ClickCommentCtl new];
        comment.deleget = self;
        comment.url = islanComment;
        comment.editDic = self.dataAry[indexPath.row];
        comment.Id = editCell.Id;

        [self.navigationController pushViewController:comment animated:YES];
    }
    
    if ([editCell.type isEqualToString:@"DiaochangPolad"]) {//新添加钓场
        IslanInforChangeCtl *change = [IslanInforChangeCtl new];
        change.editDic = self.dataAry[indexPath.row];
        change.isYuJuDian = NO;
        change.deleget = self;
        change.number=30;
        [self.navigationController pushViewController:change animated:YES];
    }
    NSLog(@"%@",editCell.type);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.dataAry removeObjectAtIndex:indexPath.row];//移除数据源的数据
        
        [self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
        [self.dataAry writeToFile:[Manager editeFilePath] atomically:YES];
    });

}

/**
 *  重发
 */
-(void)anewBtnClickWhithCell:(UITableViewCell *)cell{

    PersonEditTableViewCell *editCell = (PersonEditTableViewCell *)cell;
    
    NSIndexPath *indexPath = [self.tableview indexPathForCell:cell];
    
    if ([editCell.type isEqualToString:@"DcUpImage"]) {//钓场传图
        ImageToController *UpImage = [ImageToController new];
        
        UpImage.dId = editCell.Id;
        UpImage.deleget = self;
        [self.navigationController pushViewController:UpImage animated:YES];
        UpImage.isyujudian = editCell.isyujud.integerValue;
        UpImage.editDic = self.dataAry[indexPath.row];
        UpImage.isAnew = YES;
    
    }
    
    if ([editCell.type isEqualToString:@"DcComment"]) {//钓场点评
        ClickCommentCtl *comment = [ClickCommentCtl new];
        comment.deleget = self;
        
        if (editCell.isyujud.integerValue==1) {
            comment.url = dianpuComment;
        }else{
            comment.url = islanComment;
        }
        
        comment.editDic = self.dataAry[indexPath.row];
        comment.Id = editCell.Id;
        [self.navigationController pushViewController:comment animated:YES];
        comment.isAnew = YES;
    }

    
    if ([editCell.type isEqualToString:@"DiaochangPolad"]) {//新添加钓场
        IslanInforChangeCtl *change = [IslanInforChangeCtl new];
        change.editDic = self.dataAry[indexPath.row];
        change.isYuJuDian = editCell.isyujud.integerValue;
        change.deleget = self;
        if (editCell.isyujud.integerValue==1) {
            change.number=40;;
        }else{
            change.number=30;;
        }
        change.number=30;
        [self.navigationController pushViewController:change animated:YES];
        change.isAnew = YES;
    }
    
    if ([editCell.type isEqualToString:@"PlusUp"]) {//发帖
        PlusSignController *plus = [PlusSignController new];
        plus.editDic = self.dataAry[indexPath.row];
        plus.fid = editCell.Id;
        plus.deleget = self;
        [self presentViewController:plus animated:YES completion:nil];
        plus.Anew = @"YES";
        
    }


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
        [self.dataAry removeObjectAtIndex:indexPath.row];//移除数据源的数据
        
        [self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
        [self.dataAry writeToFile:[Manager editeFilePath] atomically:YES];
    });
   
}

-(void)upLoadeImageProgress:(CGFloat)progress{
    [self hudShowWhithProgress:progress];
}

-(void)upLoadfailure{

    [self.MyHUD hide:YES];
    [self GetToEdit];
}

-(void)CommentupLoadeImageProgress:(CGFloat)progress{

    [self hudShowWhithProgress:progress];
}

-(void)CommentupLoadfailure{
 [self.MyHUD hide:YES];
    [self GetToEdit];
}

-(void)hudShowWhithProgress:(CGFloat)progress{
    
    [self.view bringSubviewToFront:self.MyHUD];
    self.MyHUD.labelText = [NSString stringWithFormat:@"%.f%%",progress*100];
    
    self.MyHUD.progress = progress;
    
    [self.MyHUD show:YES];
    
    
    if (progress==1) {
        
        [self.MyHUD hide:YES];
        
    }
}


-(void)GetToEdit{
    
self.dataAry = [NSMutableArray arrayWithContentsOfFile:[Manager editeFilePath]];
    [self.tableview reloadData];
    
}

@end
